package com.shankungfu.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.R;
import com.shankungfu.bean.CommentDataBean;
import com.shankungfu.bean.PersonalLetterDataBean;
import com.shankungfu.bean.SystemDataBean;
import com.shankungfu.util.PhoneUtils;
import com.shankungfu.view.HorizontalListView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private List<Object> childrens;
	private ArrayList<Map<Object, Object>> groups;
	private LayoutInflater inflater;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public MyExpandableListAdapter(Context context, List<Object> childrens,
			ArrayList<Map<Object, Object>> groups, DisplayImageOptions options) {
		this.context = context;
		this.childrens = childrens;
		this.groups = groups;
		this.options = options;
		imageLoader = ImageLoader.getInstance();
	}

	public void setGroupList(ArrayList<Map<Object, Object>> groups) {
		this.groups = groups;
	}

	public void setChildrens(List<Object> childrens) {
		this.childrens = childrens;
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (groups.size() > 0) {
			switch (groupPosition) {
			case 0:
				ArrayList<CommentDataBean> commentList = (ArrayList<CommentDataBean>) childrens
						.get(0);
				return commentList != null ? commentList.size() : 0;
			case 1:
				ArrayList<PersonalLetterDataBean> personalList = (ArrayList<PersonalLetterDataBean>) childrens
						.get(1);
				return personalList != null ? personalList.size() : 0;
			case 2:
				ArrayList<SystemDataBean> systemList = (ArrayList<SystemDataBean>) childrens
						.get(2);
				return systemList != null ? systemList.size() : 0;
			}
		}

		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return groups.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		GroupViewHolder holder = null;
		if (convertView == null) {
			holder = new GroupViewHolder();
			convertView = View.inflate(context,
					R.layout.message_group_item_layout, null);
			holder.switchImage = (ImageView) convertView
					.findViewById(R.id.switch_image);
			holder.message_icon = (ImageView) convertView
					.findViewById(R.id.message_icon);
			holder.message_category = (TextView) convertView
					.findViewById(R.id.message_category);
			holder.number = (TextView) convertView.findViewById(R.id.number);

			convertView.setTag(holder);
		} else {
			holder = (GroupViewHolder) convertView.getTag();
		}

		Map<Object, Object> map = groups.get(groupPosition);

		if (isExpanded) {
			holder.switchImage
					.setBackgroundResource(R.drawable.message_arrow_up);

			switch (groupPosition) {
			case 0:
				// sendData(map.get(""d));
				break;
			case 2:
				// sendData(2);
				break;
			}

		} else {
			holder.switchImage
					.setBackgroundResource(R.drawable.message_arrow_down);
		}

		holder.message_icon.setImageResource((Integer) (map.get("image")));
		holder.message_category.setText((String) (map.get("name")));

		int number = (Integer) map.get("number");
		if (number != 0) {
			holder.number.setText(number + "");
		} else {
			holder.number.setVisibility(View.GONE);
		}

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null;
		if (convertView == null) {
			holder = new ChildViewHolder();
			convertView = View.inflate(context,
					R.layout.message_child_item_layout, null);
			holder.message_child_author = (TextView) convertView
					.findViewById(R.id.message_child_author);
			holder.message_child_date = (TextView) convertView
					.findViewById(R.id.message_child_date);
			holder.message_child_content = (TextView) convertView
					.findViewById(R.id.message_child_content);
			holder.message_child_user_pic = (ImageView) convertView
					.findViewById(R.id.message_child_user_pic);
			holder.pic_listview = (HorizontalListView) convertView
					.findViewById(R.id.pic_listview);

			convertView.setTag(holder);
		} else {
			holder = (ChildViewHolder) convertView.getTag();
		}

		String username = "";
		String time = "";
		String detail = "";
		String imageUrl = "";
		boolean isNew = false;
		
		holder.pic_listview.setVisibility(View.GONE);
//		holder.message_child_content.setMovementMethod(LinkMovementMethod
//				.getInstance());

		switch (groupPosition) {
		case 0: // 评论消息

			List<CommentDataBean> commentDataBeans = (List<CommentDataBean>) childrens
					.get(0);
			CommentDataBean commentDataBean = commentDataBeans
					.get(childPosition);

			username = commentDataBean.getUser_name();
			time = PhoneUtils.TimeStamp2DateForSKF(commentDataBean
					.getReply_time());
			detail = commentDataBean.getMessage();
			imageUrl = commentDataBean.getAvatar_img();
			isNew = commentDataBean.isIsnew();

			holder.message_child_content.setEnabled(false);
			
			
			break;
		case 1: // 私信消息
			List<PersonalLetterDataBean> personalLetterBeans = (List<PersonalLetterDataBean>) childrens
					.get(1);
			PersonalLetterDataBean personalLetterBean = personalLetterBeans
					.get(childPosition);

			username = personalLetterBean.getUser_name();
			time = personalLetterBean.getPersonal_letter_time();
			detail = personalLetterBean.getPersonal_letter_content();
			imageUrl = personalLetterBean.getUser_img_url();
			isNew = personalLetterBean.isIsnew();

			holder.message_child_content.setEnabled(true);
			
			break;
		case 2: // 系统消息
			List<SystemDataBean> systemBeans = (List<SystemDataBean>) childrens
					.get(2);

			SystemDataBean systemBean = systemBeans.get(childPosition);

			username = systemBean.getUser_name();
			time = PhoneUtils.TimeStamp2DateForSKF(systemBean.getAdd_time());
			detail = systemBean.getMessage();
			imageUrl = systemBean.getAvatar();
			isNew = systemBean.isIsnew();
			holder.message_child_content.setEnabled(false);

			break;
		}

		holder.message_child_author.setText(username);

		if (TextUtils.isEmpty(time)) {
			holder.message_child_date.setVisibility(View.GONE);
		} else {
			holder.message_child_date.setText("时间:" + time);
		}

		if (isNew) {
			holder.message_child_content.setTextColor(context.getResources()
					.getColor(R.color.pickitem_project_text));
		} else {
			holder.message_child_content.setTextColor(context.getResources()
					.getColor(R.color.setting_text_color));
		}

		holder.message_child_content.setText(Html.fromHtml(detail));

		imageLoader.displayImage(imageUrl, holder.message_child_user_pic,
				options, animateFirstListener);

		return convertView;
	}

	private class GroupViewHolder {
		private Button group;
		private ImageView switchImage;
		private ImageView message_icon;
		private TextView message_category;
		private TextView number;
	}

	private class ChildViewHolder {
		private TextView message_child_author;
		private TextView message_child_date;
		private TextView message_child_content;
		private ImageView message_child_user_pic;
		private HorizontalListView pic_listview;
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
